package com.panfeng.xcloud.boss.provider.member.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;

@Configuration
@EnableAsync
public class AsyncExecutorConfig {

	@Value("${threadpool.thread.poolSize:50}")
	private String corePoolSize;
	
	@Value("${threadpool.thread.maximumPoolSize:500}")
	private String maxPoolSize;
	
	@Value("${threadpool.thread.poolSize:50}")
	private String poolSize;
	
	@Value("${threadpool.thread.maximumPoolSize:500}")
	private String maximumPoolSize;
	
	@Value("${threadpool.thread.acceptTaskCount:-1}")
	private String acceptTaskCount;

	@Bean("asyncExecutor")
	public Executor asyncExecutor() {
		ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
		executor.setCorePoolSize(Integer.valueOf(corePoolSize));
		executor.setMaxPoolSize(Integer.valueOf(maxPoolSize));
		executor.setQueueCapacity(1000);
		executor.setThreadNamePrefix("AsyncExecutor-");
		executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
		executor.initialize();
		return executor;
	}
	
}